package com.tuzhi.util;

import java.io.*;

public class CloneTzUtil {

	public static <T extends Serializable> T clone(Object obj) throws Exception {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bout);
		oos.writeObject(obj);

		ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
		ObjectInputStream ois = new ObjectInputStream(bin);
		return (T) ois.readObject();
	}

	public static <T extends Serializable> T cloneObj(Object obj){
		T t = null;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		try {
			ObjectOutputStream oos = new ObjectOutputStream(bout);
			oos.writeObject(obj);

			ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
			ObjectInputStream ois = new ObjectInputStream(bin);
			t =  (T) ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return t;
	}

}
